package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.BomItem;
import com.efast.dmom.mes.bean.BomItemTree;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.BomItemDataHandler;
import com.efast.dmom.mes.entity.BomItemEntity;
import com.efast.dmom.mes.service.BomItemService;

import jodd.util.StringUtil;

@Service
public class BomItemServiceImpl implements BomItemService {

	@Autowired
	private BomItemDataHandler bomItemHandler;

	@Override
	public ResultBean save(BomItem bomItem) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (bomItem.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(bomItem.getInnerData().getBomName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查BomItem名称是否填写!");
			return resultBean;
		}
		if (bomItem.getInnerData().getBomId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查bomId是否填写!");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(bomItem.getInnerData().getPartNo())
				|| !StringUtil.isNotBlank(bomItem.getInnerData().getPartRevision())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料号、物料版本是否填写!");
			return resultBean;
		}
		if (bomItem.getInnerData().getQty() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数量不能为0!");
			return resultBean;
		}
		BomItem bomItemByName = bomItemHandler.getByName(bomItem.getInnerData().getBomName());

		if (bomItemByName.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("名称不唯一!");
			return resultBean;
		}

		BomItem bomItemData = bomItemHandler.getBomItemByPartNoAndRevision(bomItem);
		if (bomItemData.getInnerData() != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该物料在BOM下已存在!");
			return resultBean;
		}
		bomItemHandler.save(bomItem);
		resultBean.setSuccess(true);
		resultBean.setMessage("BomItem新增成功!");
		return resultBean;
	}

	@Override
	public ResultBean update(BomItem bomItem) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if (bomItem.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(bomItem.getInnerData().getBomName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查BomItem名称是否填写!");
			return resultBean;
		}
		if (bomItem.getInnerData().getBomId() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查bomId是否填写!");
			return resultBean;
		}
		if (!StringUtil.isNotBlank(bomItem.getInnerData().getPartNo())
				|| !StringUtil.isNotBlank(bomItem.getInnerData().getPartRevision())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("请检查物料名称、物料版本是否填写!");
			return resultBean;
		}
		if (bomItem.getInnerData().getQty() == 0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("数量不能为0!");
			return resultBean;
		}
		BomItem bomItemById = bomItemHandler.getById(bomItem.getInnerData().getId());
		if (bomItemById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该BomItem不存在!");
			return resultBean;
		}
		if (!bomItem.getInnerData().getBomName().equals(bomItemById.getInnerData().getBomName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("名称不可修改!");
			return resultBean;
		}
		if (!bomItem.getInnerData().getPartNo().equals(bomItemById.getInnerData().getPartNo())
				&& !bomItem.getInnerData().getPartRevision().equals(bomItemById.getInnerData().getPartRevision())) {
			BomItem bomItemData = bomItemHandler.getBomItemByPartNoAndRevision(bomItem);
			if (bomItemData.getInnerData() != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("修改的物料在BOM下已存在!");
				return resultBean;
			}
		}
		bomItemHandler.update(bomItem);
		resultBean.setSuccess(true);
		resultBean.setMessage("BomItem修改成功!");
		return resultBean;
	}

	@Override
	public ResultBean delete(BomItem bomItem) {
		ResultBean resultBean = new ResultBean(true, ' ');
		bomItemHandler.delete(bomItem);
		resultBean.setSuccess(true);
		resultBean.setMessage("BomItem删除成功!");
		return resultBean;
	}

	@Override
	public List<BomItem> getByBomId(long bomId) {
		return bomItemHandler.getByBomId(bomId);
	}

	@Override
	public BomItemTree getBomItemByBomId(BomItem bomItem,int start,int length) {
		
		
		
		List<BomItemEntity> bomItemList = bomItemHandler.getBomItemByBomId(bomItem,start,length);
		
		List<BomItem> bomItemSize = bomItemHandler.getByBomId(bomItem.getInnerData().getBomId());
		
		
		BomItemTree bomItemTree = new BomItemTree();
		if (bomItemList != null && bomItemSize != null) {
			bomItemTree.setCountSize(bomItemSize.size());
			bomItemTree.setItemList(bomItemList);
		}
		return bomItemTree;
	}

	@Override
	public BomItemTree getBomItemByBomId(BomItem bomItem) {
		// TODO Auto-generated method stub
		return null;
	}

}
